package com.example.zce.library.data.sys.service;

import com.example.zce.library.common.bean.ResultTableBean;
import com.example.zce.library.common.utils.UUIDUtils;
import com.example.zce.library.data.sys.constant.BackRoleEnum;
import com.example.zce.library.data.sys.dao.BackRoleMapper;
import com.example.zce.library.data.sys.dao.BackRoleMenuMapper;
import com.example.zce.library.data.sys.dao.BackUserMapper;
import com.example.zce.library.data.sys.entity.BackRole;
import com.example.zce.library.data.sys.entity.BackRoleMenu;
import com.example.zce.library.data.sys.entity.BackUser;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：
 *
 * @author zengyz
 * @date 2018/7/29 14:18
 */
@Service
public class BackRoleService {

    @Autowired
    private BackRoleMapper backRoleMapper;

    @Autowired
    private BackUserMapper backUserMapper;

    @Autowired
    private BackRoleMenuMapper backRoleMenuMapper;

    public ResultTableBean<BackRole> findByParams(Map params) {
        PageHelper.startPage(Integer.valueOf(params.get("pageNum").toString()), Integer.valueOf(params.get("pageSize").toString()));
        List<BackRole> backRoles = backRoleMapper.selectListByParams(params);
        PageInfo<BackRole> pageInfo = new PageInfo<>(backRoles);
        ResultTableBean<BackRole> resultTableBean = new ResultTableBean<>(0,backRoles,pageInfo.getTotal());
        return resultTableBean;
    }

    public List<BackRole> findByAll() {
        Map params=new HashMap();
        params.put("state",BackRoleEnum.STATE_OK.getIntegerValue());
        List<BackRole> backRoles = backRoleMapper.selectListByParams(params);
        return backRoles;
    }

    public BackRole findById(String id){
        return backRoleMapper.selectByPrimaryKey(id);
    }

    @Transactional
    public int saveOrUpdate(BackRole backRole) throws Exception{
        if (StringUtils.isNotEmpty(backRole.getId())){
            return backRoleMapper.updateByPrimaryKeySelective(backRole);
        }else{
            backRole.setCreateTime(new Date());
            backRole.setId(UUIDUtils.getUUID32());
            return backRoleMapper.insert(backRole);
        }
    }

    @Transactional
    public int delete(String id) throws Exception{
        Map params=new HashMap();
        params.put("roleId",id);
        List<BackUser> backUsers = backUserMapper.selectListByParams(params);
        if (backUsers.isEmpty()){
            return backRoleMapper.deleteByPrimaryKey(id);
        }else{
            for (BackUser backUser:backUsers) {
                backUser.setRoleId(BackRoleEnum.GUEST_ROLE_ID.getStringValue());
                backUserMapper.updateByPrimaryKeySelective(backUser);
            }
            return backRoleMapper.deleteByPrimaryKey(id);
        }
    }

    @Transactional
    public int insertRoleMenu(String roleId,String [] menuIds) throws Exception{
        int delete = backRoleMenuMapper.delete(roleId);
        for (String menuId:menuIds) {
            if (StringUtils.isNotEmpty(menuId)){
                BackRoleMenu backRoleMenu=new BackRoleMenu();
                backRoleMenu.setRoleId(roleId);
                backRoleMenu.setMenuId(menuId);
                backRoleMenuMapper.insert(backRoleMenu);
            }
        }
        return 1;
    }

}
